package com.zt.rongda.stock.service;

import java.text.SimpleDateFormat;
import java.util.List;

import com.zt.rongda.stock.inter.StockMapper;
import com.zt.rongda.stock.model.Stock;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StockService extends BaseService {

    @Autowired
    private StockMapper stockMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select * from STOCK where 1=1 ");

        sqlBuilder.addWhereLike2("STOCK_NAME", reqBean.getInfo("stockName"));
        sqlBuilder.addWhereLike2("TYPE_NAME", reqBean.getInfo("typeName"));

        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * 模糊查询
     *
     * @param reqBean
     * @return
     */
    public JSONArray queryFuzzy(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");
        sqlBuilder.appendSql("select * from STOCK where 1=1 ");

        sqlBuilder.addWhereLike2("STOCK_NAME", reqBean.getInfo("stockName"));
        sqlBuilder.addWhereLike2("TYPE_NAME", reqBean.getInfo("typeName"));

        return query(sqlBuilder);
    }

    /**
    * 通过id查询
    *
    * @param stockId
    * @return
    */
    public Stock getById(String stockId) {
        return stockMapper.selectByPrimaryKey(stockId);
    }

    /**
    * 新增
    *
    * @param stock
    */
    public void add(Stock stock) {
        stock.setStockId(stringId());// 手动设置Id
        stockMapper.insert(stock);
    }

    /**
    * 修改
    *
    * @param stock
    */
    public void update(Stock stock) {
        String stockId = stock.getStockId();
        if (StringUtil.isEmpty(stockId)) {
            throw new BusinessException("获取主键失败");
        }

        stockMapper.updateByPrimaryKeySelective(stock);
    }

    /**
    * 删除
    *
    * @param stockId
    */
    public void delete(String stockId) {
        if (StringUtil.isEmpty(stockId)) {
            throw new BusinessException("获取主键失败");
        }
        stockMapper.deleteByPrimaryKey(stockId);
    }

    /**
     * 库存增加
     *
     * @param stock
     */
    public Integer increase(Stock stock) {
        String sql = "update STOCK AS a set a.AMOUNT = a.AMOUNT + ? WHERE a.STOCK_ID = ?";
        this.update(sql,new Object[]{stock.getAmount(), stock.getStockId()});
        return 0;
    }

    /**
     * 库存减少
     *
     * @param stock
     */
    public Integer decrease(Stock stock) {
        String sql = "update STOCK AS a set a.AMOUNT = a.AMOUNT - ? WHERE a.STOCK_ID = ?";
        this.update(sql,new Object[]{stock.getAmount(), stock.getStockId()});
        return 0;
    }

    /**
     * 材料是否存在
     *
     * @param stock
     */
    public boolean isExisted(Stock stock) {
        String sql = "SELECT COUNT(*) num FROM STOCK AS a WHERE a.STOCK_NAME = ? AND  a.TYPE_ID = ? AND a.TYPE_NAME = ? AND a.BRAND = ? AND a.SPEC = ? AND a.UNIT = ?";
        JSONArray jaNum = this.query(sql,new Object[]{stock.getStockName(),stock.getTypeId(),stock.getTypeName(),stock.getBrand(),stock.getSpec(),stock.getUnit()});
        String num = jaNum.getJSONObject(0).getString("num");
        if("0".equals(num)){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 库存是否充足
     *
     * @param stock
     */
    public boolean isEnough(Stock stock) {
        String sql = "select AMOUNT amount from STOCK where STOCK_ID = ?";
        Integer amount = Integer.parseInt(this.query(sql, new Object[]{stock.getStockId()}).getJSONObject(0).getString("amount") );
        if(stock.getAmount() > amount){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 库存更新
     *
     * @param stock
     */
    public Integer stockUpdate(Stock stock) {
        //判断材料是否存在
        String sql = "SELECT COUNT(*) num FROM STOCK AS a WHERE a.STOCK_NAME = ? AND  a.TYPE_ID = ? AND a.TYPE_NAME = ? AND a.BRAND = ? AND a.SPEC = ? AND a.UNIT = ?";
        JSONArray jaNum = this.query(sql,new Object[]{stock.getStockName(),stock.getTypeId(),stock.getTypeName(),stock.getBrand(),stock.getSpec(),stock.getUnit()});
        String num = jaNum.getJSONObject(0).getString("num");
        if("0".equals(num)){
            return 1;  //更新材料不存在
        }else{
            sql = "select NUM oldNum from STOCK_TRANSFER_IN where LIST_ID = ?";
            Integer oldNum = Integer.parseInt(this.query(sql, new Object[]{stock.getListId()}).getJSONObject(0).getString("oldNum") );
            sql = "update STOCK AS a set a.AMOUNT = a.AMOUNT + ? - ? WHERE a.STOCK_ID = ?";
            this.update(sql,new Object[]{stock.getAmount(), oldNum, stock.getStockId()});
            return 0; // 成功
        }
    }



}